﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Disposables;
using System.Linq.Expressions;
using System.Windows.Data;
using SoftSource.Common;
using SoftSource.Timecard.ViewModelInterfaces;
using SoftSource.Timecard.Web.Data;

namespace SoftSource.Timecard.ViewModels
{
    public class EntriesFlaggedForAdminViewModel : IEntriesFlaggedForAdminViewModel
    {
        #region Fields
        private CompositeDisposable compositeDisposable;
        #endregion Fields

        public EntriesFlaggedForAdminViewModel()
        {
            this.compositeDisposable = new CompositeDisposable();
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region IDataViewModel Members

        public IObservable<Unit> Load { get; set; }

        #endregion

        #region IEntriesFlaggedForAdminViewModelConsume Members

        private PagedCollectionView entries;
        public PagedCollectionView Entries
        {
            get { return this.entries; }
            set
            {
                this.entries = value;
                this.entries.SortDescriptions.Add(new SortDescription(
                    ObjectReflection.GetPropertyName<EntryLog>(entry => entry.EntryDate),
                    ListSortDirection.Descending));
                this.entries.MoveCurrentToFirst();

                this.PropertyChanged.Notify(() => this.Entries);
            }
        }

        #endregion

        #region IEntriesFlaggedForAdminViewModelService Members

        private IEnumerable<EntryLog> entrySource;
        public IEnumerable<EntryLog> EntrySource
        {
            get { return this.entrySource; }
            set
            {
                this.entrySource = value;
                this.Entries = new PagedCollectionView(this.entrySource);
            }
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            this.compositeDisposable.Dispose();
            GC.SuppressFinalize(this);
        }

        #endregion

        ~EntriesFlaggedForAdminViewModel()
        {
            this.Dispose();
        }
    }
}
